package kessai;

import DBs.DBExec;
import kaiin.KaiinBeans;

import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BuySend extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException{
		this.doPost(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException{

		req.setCharacterEncoding("utf-8");

		//日付取得用オブジェクト
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String sql = "";

		HttpSession session = req.getSession(true);
		DBExec dbe = new DBExec();

		KaiinBeans kbean = (KaiinBeans)session.getAttribute("kaiin");

		CartBeans cbean = (CartBeans)session.getAttribute("cart");
		cbean.setTyuumon_id("5555");

		for(int i=0; i<cbean.getItems().size(); i++){
			sql = "insert into uriage " +
					"values('" +
					cbean.getTyuumon_id() + "', " +
					sdf.format(d) + ", " +
					"'" + kbean.getUser_id() + "', " +
					cbean.getItems().get(i).getSyouhin_id() + ", " +
					cbean.getItems().get(i).getNedan() + ", " +
					cbean.getItems().get(i).getKosuu() + ", " +
					cbean.getSyoukei() + ", " +
					cbean.getSougoukei() + ");";

			dbe.updateTable(sql);
		}

		String message = "<p>注文を承りました！</p>" + sql;
		String title = "注文完了";

		req.setAttribute("message", message);
		req.setAttribute("title", title);

		RequestDispatcher dp = req.getRequestDispatcher("/src/jsp/thanks.jsp");

		dp.forward(req, res);

	}
}